<?php

namespace App\Http\Controllers\Plugin;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
//use Modules\Member\Entities\SmsLog;
trait SmsController
{

    private $accessKeyId;
    private $accessKeySecret;
    private $domain = 'dysmsapi.aliyuncs.com';

    public function __construct()
    {
        $this->accessKeyId = config('sms.defaults.accessKeyId');
        $this->accessKeySecret = config('sms.defaults.accessKeySecret');
    }

    public function sendSms($phone, $model, $parm)
    {
        $config = config("sms.model.$model");
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => $config['sign'],//$this->sign,
                        'TemplateCode' => $config['temple'],//$this->tempCode,
                        'TemplateParam' => json_encode($parm),//"{\"code\":\"1111\"}",
                    ],
                ])
                ->request();
            //添加短信发送记录
            //SmsLog::create(['mobile'=>$phone,'sms_model'=>$model,'parm'=>json_encode($parm)]);
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }
}
